Skip to content

Add the craft-cloud asset transform driver - #213

Draft
timkelty wants to merge 2 commits into
5.xfrom
feature/craft-6-cloud-transforms
Draft

timkelty wants to merge 2 commits into
5.xfrom
feature/craft-6-cloud-transforms

Conversation

@timkelty

@timkelty timkelty commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This adds a craft-cloud asset transform driver. Transforms become signed Cloudflare image URLs on the Cloud CDN, and the gateway verifies the signature before it transforms anything.

On Craft Cloud, the provider sets the driver up for every volume:

  • It defines a craft-cloud transformer, unless the project already defines one.
  • It sets defaultTransformer to craft-cloud, unless the project already set one.

Off Cloud, nothing changes unless a project opts in itself:

// config/craft/general.php
->assets(AssetsConfig::create()
    ->addTransformer('craft-cloud', ['driver' => 'craft-cloud'])
    ->defaultTransformer('craft-cloud') // or set it in each volume's Asset Transformer setting
)
AssetTransformers::transform($asset, $definition)
  forVolume()                           # the volume's transformer, or the defaultTransformer
  CloudAssetTransformDriver::transform()
    no public URLs, or not on S3        # hands off to transformer('craft') with only the core parameters
    unsupported file type               # throws NotSupportedException, so the CP shows a file icon
    CloudflareOptions::fromParameters()
    AssetCdn::transformUrl()
      puts the options in the asset URL's query
      signs @path and @query into s     # with CRAFT_CLOUD_SIGNING_KEY
      appends v after signing           # only with assets.revUrls; the gateway leaves v out

Parameter mapping

core parameter               Cloudflare option
width, height, quality    →  same names
mode + upscale            →  fit           # images.upscale applies when upscale isn't set
position                  →  gravity       # crops use the focal point, like the craft driver
fill                      →  background    # letterbox only
format jpg                →  baseline-jpeg, or jpeg when interlaced
PDF without a format      →  format=auto   # the gateway only rasterizes PDFs when a format is set

Behavior

  • Cloudflare-only options. The driver declares these as parameterRules, so core validates them:

    • anim, background, blur, border, brightness, compression, contrast, dpr, draw, flip, gamma, gravity, metadata, rotate, saturation, segment, sharpen, trim and zoom
    • the gateway's PDF page

    You can set them inline, or on named transforms under the transformer's name. They have no CP fields for beta.

  • Which assets. The driver transforms every public asset on S3 in the volumes that use it1.

    • It transforms avif, gif, heic, heif, jpeg, jpg, png, svg, webp and pdf. GIFs and SVGs also depend on images.transformGifs and images.transformSvgs.
    • Volumes without public URLs, such as temporary uploads, and disks that aren't S3 go to the craft transformer. Core's docs recommend this handoff for a default transformer.
  • Results. Each result reports its output width and height, calculated the way the craft driver does it, so getImg() and x srcsets work.

  • Signing. CraftCms\Cloud\Signing\RequestSigner is shared with Add static page caching for Craft 6 on Cloud #214.

Dependencies

Proposed gateway change

If the gateway rasterized PDFs without a requested format, as it already does for images, the driver wouldn't need the format=auto special case for PDFs.

Evidence

  • Before: on 5.x, a project that defines the transformer can't use it:
    addTransformer('cdn', ['driver' => 'craft-cloud'])
    transformer('cdn')
    → AssetTransformDriverNotFoundException: Asset transformer [cdn] uses the [craft-cloud] driver, which is not registered.
    
    After: transformer('cdn') returns a CloudAssetTransformDriver. This is a 1200×750 WebP crop of a 4000×3000 photo with a focal point:
    url      starts with {CDN}/tests/photo.jpg?
    query    = width=1200, height=750, format=webp, fit=cover, gravity={x: 0.57, y: 0.7707}, s=…
    result   = image/webp, 1200×750
    
    the gateway verifies the URL with the signing key       ✓
    a PDF crop gets format=auto and reports image/png       ✓
    a local disk goes to craft with only {mode, width}      ✓
    archive.tiff, or a GIF with transformGifs off, throws NotSupportedException ✓
    
    composer ci (Pint, PHPStan, Pest):
    {"tool":"pint","result":"passed"}
    [OK] No errors
    Tests:    34 passed (91 assertions)
    

Related issues

Footnotes

  1. An earlier revision also checked the bucket. Add Craft Cloud storage disks for Craft 6 #212's craft-cloud disks are always S3 on the project's bucket, and a bucket check can't tell a dev bucket from Cloud's. Transforms on the CDN work wherever the original asset URLs do. ↩

timkelty added a commit that referenced this pull request Nov 10, 2026
Tag pages with their Craft cache tags and derive CDN-Cache-Control from
the final response headers. Purge on every TagDependency invalidation,
except drafts and revisions, with one queued PurgeStaticCache job per
request, command or queue job, signed with #213's RequestSigner. Add a
Clear Caches option and a single cloud:purge command.

Require craftcms/cms ^6.0.0-beta.1.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-6551416a-ce7c-4e90-adac-8c2edc72248c
timkelty added a commit that referenced this pull request Nov 10, 2026
Tag pages with their Craft cache tags and derive CDN-Cache-Control from
the final response headers. Purge on every TagDependency invalidation,
except drafts and revisions, with one queued PurgeStaticCache job per
request, command or queue job, signed with #213's RequestSigner. Add a
Clear Caches option and a single cloud:purge command.

Require craftcms/cms ^6.0.0-beta.1.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-6551416a-ce7c-4e90-adac-8c2edc72248c
timkelty added a commit that referenced this pull request Nov 10, 2026
Tag pages with their Craft cache tags and derive CDN-Cache-Control from
the final response headers. Purge on every TagDependency invalidation,
except drafts and revisions, with one queued PurgeStaticCache job per
request, command or queue job, signed with #213's RequestSigner. Add a
Clear Caches option and a single cloud:purge command.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-6551416a-ce7c-4e90-adac-8c2edc72248c
@timkelty timkelty changed the title Add the Craft Cloud asset transform driver Add Cloud asset transforms for Craft 6 Nov 19, 2026
@timkelty timkelty changed the title Add Cloud asset transforms for Craft 6 Add the craft-cloud asset transform driver Nov 19, 2026
@timkelty
timkelty force-pushed the feature/craft-6-cloud-transforms branch 2 times, most recently from 3692144 to fbdc571 Compare December 5, 2026 16:10
Only when the project hasn't chosen one, and without replacing a craft-cloud
transformer the project defined. Volumes without public URLs, such as
temporary uploads, now go to the craft transformer, since the default reaches
them too.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1b2a5-ffcf-7312-966f-7e8624cf8b79
@timkelty
timkelty force-pushed the feature/craft-6-cloud-transforms branch from b48a616 to b9446ba Compare December 6, 2026 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant